<?php

namespace Tuobaye\FilamentAdmin\Resources;


use Filament\Facades\Filament;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Tuobaye\FilamentAdmin\Resources\UserResource\Pages;
use Tuobaye\FilamentAdmin\Concerns\WithPermissions;
use Tuobaye\FilamentAdmin\Contracts\HasPermissions;
use Tuobaye\FilamentAdmin\Models\User;

class UserResource extends Resource implements HasPermissions
{
    use Tables\Concerns\InteractsWithTable;

    use WithPermissions;

    protected static ?string $model = User::class;

    protected static ?string $label = '管理员';

    protected static ?string $navigationIcon = 'heroicon-o-users';

    protected static ?string $slug = 'admin/users';

    public static function getNavigationGroup(): ?string
    {
        return config('filament-admin.resource.group');
    }


    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('username')->label('用户名')->required()->unique(ignoreRecord: true),
                Forms\Components\TextInput::make('password')->label('密码')->password()->required(fn($record) => !$record)->revealable(),
                Forms\Components\TextInput::make('name')->label('姓名'),
                Forms\Components\Toggle::make('status')->label('启用')->inline(false)->default(true),
                Forms\Components\Toggle::make('is_super_admin')->label('超级管理员')->inline(false)->default(false)->reactive(),
                Forms\Components\Select::make('roles')->label('用户组')->relationship('roles', 'name')->multiple()->preload()->columnSpan(2)
                    ->visible(fn(Forms\Get $get) => !$get('is_super_admin')),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('id')->label('ID')->sortable(),
//                Tables\Columns\ImageColumn::make('avatar')->label('头像'),
                Tables\Columns\TextColumn::make('username')->label('用户名')->searchable(),
                Tables\Columns\IconColumn::make('is_super_admin')->label('超级管理员')->boolean(),
                Tables\Columns\TextColumn::make('status')->label('状态')->badge(),
                Tables\Columns\TextColumn::make('created_at')->label('创建时间')->sortable()
            ])
            ->filters([
                //
            ])
            ->actions([
//                Tables\Actions\Action::make('roles')->label('用户组')->url(fn($record): string => static::getUrl('roles', ['record' => $record])),
                Tables\Actions\EditAction::make()->using(function ($record, array $data) {
                    if (!$data['password']) {
                        unset($data['password']);
                    }
                    $record->update($data);
                    return $record;
                }),
                Tables\Actions\DeleteAction::make()->using(function ($record) {
                    if ($record->id === Filament::auth()->id()) {
                        Notification::make()->title('不能删除当前已登录账号')->danger()->send();
                        return false;
                    }
                    return $record->delete();
                })
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])->defaultSort('id', 'DESC');
    }

    public static function getRelations(): array
    {
        return [
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListUsers::route('/'),
//            'create' => Pages\CreateUser::route('/create'),
//            'edit' => Pages\EditUser::route('/{record}/edit'),
        ];
    }


}
